package com.renjibo.childred;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;

import com.renjibo.childred.databases.MySqlite;
import com.renjibo.childred.fragments.ClassFragment;
import com.renjibo.childred.fragments.HomeFragment;
import com.renjibo.childred.fragments.MybuyFragment;
import com.renjibo.childred.fragments.ShoppingFragment;
import com.renjibo.childred.utils.TimeUtils;
import com.renjibo.childred.views.NewsFragmentTabHost;
import com.zhy.autolayout.AutoLayoutActivity;

public class MainActivity extends AutoLayoutActivity {
    private String []arr={"首页","分类","购物车","我的"};
    private int []images={R.drawable.home_image,R.drawable.class_image,R.drawable.shopping_image,R.drawable.mybuy_image};
    private Class fragments[]={HomeFragment.class, ClassFragment.class, ShoppingFragment.class, MybuyFragment.class};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new MySqlite(this).getWritableDatabase();
        initView();
    }
    public void initView(){
        final NewsFragmentTabHost newsFragmentTabHost= (NewsFragmentTabHost) findViewById(R.id.tabHost);
        newsFragmentTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
        //去掉分割线
        newsFragmentTabHost.getTabWidget().setDividerDrawable(null);
        for(int i=0;i<images.length;i++){
            TabHost.TabSpec tabSpec = newsFragmentTabHost.newTabSpec(arr[i]).setIndicator(getImageView(i));
            //添加
            newsFragmentTabHost.addTab(tabSpec,fragments[i],null);
            final  int index=i;
            newsFragmentTabHost.getTabWidget().getChildTabViewAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    newsFragmentTabHost.setCurrentTab(index);
                }
            });
            newsFragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.snow);
        }

    }
    public View getImageView(int index){
        View bottom_view= getLayoutInflater().inflate(R.layout.bottom_image,null);
        ImageView bottom_imageView= (ImageView) bottom_view.findViewById(R.id.bottom_imageView);
        bottom_imageView.setImageResource(images[index]);
        return bottom_view;
    }
}
